package com.changgou.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.util.CanalUtil;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 广告表数据监听
 * @author wangshaodi
 */
@CanalEventListener
public class AdListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ListenPoint(schema = "changgou_business",table = {"tb_ad"})
    public void adUpdate(CanalEntry.RowData rowData){
        System.out.println("tb_ad表变化前的数据");
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        Map<String, String> beforeMap = CanalUtil.convertToMap( beforeColumnsList );
        System.out.println(beforeMap);

        System.out.println("tb_ad表变化后的数据");
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        Map<String, String> afterMap = CanalUtil.convertToMap( afterColumnsList );
        System.out.println(afterMap);

        //得到原来的
        String position1 = beforeMap.get( "position" );
        if(position1!=null){
            rabbitTemplate.convertAndSend( "", "ad_update_queue",position1 );
        }

        String position2 = afterMap.get( "position" );
        if(position2!=null  && !position2.equals( position1 )){
            rabbitTemplate.convertAndSend( "", "ad_update_queue",position2 );
        }
    }
}
